$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var STANDARD = "";
    var pass = 1;

    //批号   获取 焦点
    $("input#goods_id").focus();

    //根据扫描内容 得到 信息
    $("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            //$("#goods_id").blur();
            if (!val) {
                $alert("请扫描机台或产品标签！");
                pass = 1;
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            //加载提示符 显示
            loading_show("搜索中...");
            //获取工单
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/qualitysampling/getQualitysamplingData',
                data: {
                    content: val
                },
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    pass = 1;
                    //加载提示符 隐藏
                    loading_hide();
                    if (res.success) {
                        var data = res.result;
                        $("#goods_id").val("");
                        //console.log(data);
                        $alert(val + "获取成功", 1000);
                        $(".content .type").text(data.type);
                        $(".content .batch").text(data.batch || "");
                        $(".content .workplanNumber").text(data.workplanNumber || "");
                        $(".content .inventoryName").text(data.inventoryName);
                        $(".content .inventorySpecification").text(data.inventorySpecification);
                        $(".content .inventoryCode").text(data.inventoryCode);
                        $(".content .inventorySteel").text(data.inventorySteel);
                        $(".content .inventoryDiameter").text(data.inventoryDiameter);
                        $(".content .shopName").text(data.shopName || "");
                        $(".content .machine").text(data.machine);
                    } else {
                        setTimeout(function () {
                            $("#goods_id").val("").focus();
                        }, 1400);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    //加载提示符 隐藏
                    loading_hide();
                    console.log(error);
                    setTimeout(function () {
                        $("#goods_id").val("").focus();
                    }, 1400);
                    $alert(error.responseText);
                }
            });
        }
    });

    //点击 确认
    $(".foot").on("click", ".add_receive", function () {
        if (pass == 0) {
            return
        }
        pass = 0;

        //判断数据
        if (!$(".content .type").text() || $(".content .type").text() == "空") {
            $alert("请扫描机台或产品标签！");
            pass = 1;
            return;
        }
        //获取 信息
        var type = $(".content .type").text();
        var batch = $(".content .batch").text();
        var machine = $(".content .machine").text();
        var workplanNumber = $(".content .workplanNumber").text();
        loading_show("取样中...");
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/qualitysampling/qualitysampling',
            data: {
                type: type,
                batch: batch,
                machine: machine,
                workplanNumber: workplanNumber,
            },
            dataType: "json",
            json: "callback",
            timeout: 60000,
            cache: false,
            success: function (res) {
                pass = 1;
                loading_hide();
                if (res.success) {
                    $alert("取样成功", 1400);
                    setTimeout(function () {
                        window.location.href = "qualitysampling.html";
                    }, 1300);
                    takeupPrint("qualitysampling", res.result);
                } else {
                    console.log(res);
                    $alert(res.message);
                }
            },
            error: function (error) {
                pass = 1;
                if (error.status == 0) {
                    loading_hide();
                    $alert("请求超时，请重新提交！");
                } else {
                    loading_hide();
                    console.log(error);
                    $alert(error.responseText);
                }
            }
        });
    });

});

